package com.bicon.botu.mall.order.controller;

import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.mall.order.dto.DeliverInformationDto;
import com.bicon.botu.mall.order.dto.DeliveryDto;
import com.bicon.botu.mall.order.entity.OrderDeliver;
import com.bicon.botu.mall.order.service.IOrderDeliverService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 李双喜
 * @since 2018-05-14
 */
@RestController
@RequestMapping("/V3.0.0/order-deliver")
public class OrderDeliverControllerV3 extends BaseController {
    @Autowired
    private IOrderDeliverService iOrderDeliverService;

    /**
     * 发货单
     *
     * @param storeId       商铺id
     * @param orderGoodsIds 多个分组订单id
     * @param userId        操作人的用户id
     * @return
     */
    @RequestMapping("/print")
    public BaseResult printDeliver(@RequestParam String storeId, @RequestParam String orderGoodsIds, @RequestParam String userId) {
        if (StringUtils.isAnyBlank(storeId, orderGoodsIds, userId)) {
            return new BaseResult(400, "参数不能为空");
        }
        List<DeliveryDto> deliveryDtoList = iOrderDeliverService.printDeliver(storeId, orderGoodsIds, userId);
        return new BaseResult(200, "success", deliveryDtoList);
    }

    /**
     * 批量发货
     *
     * @param jsonString  发货json
     * @param systemUserId 登录人的userId
     * @return
     */
    @RequestMapping("/batch-deliver")
    public BaseResult batchDeliver(@RequestParam String jsonString, @RequestParam String systemUserId) {
        if (StringUtils.isEmpty(jsonString)) {
            return new BaseResult(400, "参数不能为空");
        }
        Boolean result = iOrderDeliverService.batchDeliver(jsonString, systemUserId);
        if (result) {
            return new BaseResult(200, "success", result);
        }
        return new BaseResult(500, "error");
    }

    /**
     * 订单确认完成
     *
     * @param orderGoodsId 分组订单id
     * @return
     */
    @RequestMapping("/completion")
    public BaseResult orderCompletion(@RequestParam String orderGoodsId,@RequestParam String systemUserId) {
        Boolean result = iOrderDeliverService.orderCompletion(orderGoodsId,systemUserId);
        if (result) {

            return new BaseResult(200, "success", result);
        }
        return new BaseResult(500, "error");
    }

    /**
     * 发货信息
     */
    @RequestMapping("/information")
    public BaseResult deliverInformation(@RequestParam String orderGoodsId) {
        if (StringUtils.isEmpty(orderGoodsId)) {
            return new BaseResult(400, "参数不能为空");
        }
        DeliverInformationDto deliverInformationDto = iOrderDeliverService.deliverInformation(orderGoodsId);
        return new BaseResult(200, "success", deliverInformationDto);
    }


    @RequestMapping(value = "/logistics", method = RequestMethod.GET)
    public BaseResult getLogistics(@RequestParam("orderId") String orderId) {
        return sendResult200(this.iOrderDeliverService.getLogistics(orderId));
    }
}
